// pages/discern/discern.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
//var localData = require('../../data/local-data.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    big_picture: true,
    discernTitle: '正在识别中...',
    imageSrc: '',
    tag: '',
    title: '全部',
    categorys:[],
    loading: false,
    animationData: {},
    showItems: false,
    cannot: false,
    cannotall :false,
    itemsList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 解析主页传递过来的数据
    this.setData({
      imageSrc: options.image,
      tag: options.tag,
      title: options.title,
      
    //  categorys: localData.categorys
    });
    this.uploadFileOpt();
  //  console.log(options);
  //  console.log(title);
  //  console.log(tag);
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
      transformOrigin: "70% 0%",
      delay: 0
    })
    this.animation = animation
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log(res);
    if (res.from === 'button') {
      // 来自页面内转发按钮
      //console.log(res.target)
    }
    return {
      title: '拍照识物，尽在探头',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
        util.successToast("分享成功");
      },
      fail: function (res) {
        // 转发失败
        util.errorToast("分享取消");
      }
    }
  },


  /**
   * 装载图片
   */
  containers_img: function (event) {
    this.setData({
      big_picture: true
    })
  },

  /**
   * 预览图片
   */
  previewImage: function () {
    var imageSrc = this.data.imageSrc;
    if (imageSrc) {
      wx.previewImage({
        urls: [imageSrc],
      })
    }
  },

  /**
   * 执行动画
   */
  doAnimation: function () {
    for (var i = 0; i < 113; i++) {
      this.animation.rotate(-50).step();
      this.animation.rotate(0).step();
    }
    this.setData({
      animationData: this.animation.export()
    })
  },

  /**
   * 显示加载进度
   */
  showLoading: function () {
    this.setData({
      loading: true
    });
  },

  /**
   * 隐藏加载进度
   */
  hideLoading: function () {
    this.setData({
      loading: false,
    });
  },

  /**
   * 图片识别上传
   */
  uploadFileOpt: function () {
    var that = this;
    var imageSrc = that.data.imageSrc;
    var tag = that.data.tag;
    var title = that.data.title;
    console.log(tag);
    console.log(title);
    if (imageSrc && tag) {
      that.showLoading();
      util.uploadFile(api.PhotoDiscern, imageSrc, { tag: tag })
        .then(function (res) {
          that.hideLoading();
          var obj = JSON.parse(res);
          console.log(obj);
          if (obj.status == 1) {
            that.setData({
              showItems: true,
              discernTitle: obj.result.food_name,
              itemsList: obj.result.food,
            });
          } else { 
            //  console.log(title);
                 if(title=="全部"){
                   that.setData({
                     cannot: true,
                     cannotall: false,
                     discernTitle: '无法识别',
                   });
                 }
              else{
                 that.setData({
                   cannot: true,
                   cannotall: true,
                   discernTitle: '无法识别',
                 });
              }
            //无法识别
           
          }
        }, function (error) {
          that.hideLoading();
          util.errorToast("网络出错！");
          that.setData({        
            discernTitle: '亲，网络出错了',
          });
        
        });
    } else {
      that.hideLoading();
      util.errorToast("无法获取图片");
      that.setData({
        discernTitle: '图片加载错误',
      });
    }
  },

  /**
   * 关闭无法识别View
   */
  closeCannot: function () {
    this.setData({
      cannot: false
    });
  },

  /**
 * 打开物品列表View
 */
  showItems: function () {
    if (this.data.itemsList.length != 0) {
      this.setData({
        showItems: true
      });
    }
  },

  /**
   * 关闭物品列表View
   */
  closeItems: function () {
    this.setData({
      showItems: false
    });
  },

  /**
   * 跳转百科
   */
  toBaiKe: function (e) {
    var name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '../baike/baike?name=' + name,
    })
  },

})